package com.tzy.for10evalue.haoyong.excelexport.consumerservice;

import com.aebiz.baseframework4ms.utils.DateUtils;
import com.aebiz.plugins.b2b.common.dto.product.log.ExcelBatchOperateDetailLog;
import com.aebiz.plugins.b2b.common.dto.product.log.ExcelBatchOperateLog;
import com.aebiz.plugins.b2b.product.business.productdirectory.directorymgr.directory.plugins.directory_p5.service.impl.Directory_P5ServiceImpl;
import com.aebiz.plugins.b2b.product.business.productlog.service.ExcelBatchOperateLogService;
import com.tzy.for10evalue.haoyong.excelexport.ExcelBatchOperateLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ztw
 * @Description:
 * @date 2022/10/17 9:58
 */
@Slf4j
public abstract class AbstractProductBatchImportNewHandler<T> implements ProductBatchImportNewHandler<T> {

    @Autowired
    private ExcelBatchOperateLogService logService;

    @Autowired
    protected Directory_P5ServiceImpl directory_p5Service;

    @Override
    public void importDetail(String message) {
        ExcelBatchOperateLog excelLog = new ExcelBatchOperateLog();
        String result = "异常";
        String msg = "成功";
        Integer status = 1;
        Integer lineId = 0;
        try {
            T excelDto = parseObject(message);
            String keyWord = getKeyWord(excelDto);
            result = String.format("%s:%s",result,keyWord);
            String excelName = getExcelName(excelDto);
            lineId = getLineId(excelDto);
            excelLog = logService.queryByExcelName(excelName);//查询日志主表
            if (excelLog == null) {
                log.error("importDetail 未找到对应得日志名称{}", excelName);
                return;
            }
            result = execute(excelDto);
        } catch (Exception e) {
            log.error("importDetail is error:{}", e);
            //插入日志信息
            msg = e.getMessage();
            status = 2;
        }
        saveLog(status, msg, lineId, result, excelLog.getId(), 1);
    }

    protected abstract String execute(T excelDto);

    protected abstract String getExcelName(T excelDto);

    protected abstract Integer getLineId(T excelDto);

    protected abstract String getKeyWord(T excelDto);

    protected abstract T parseObject(String message);

    protected abstract void checkImportExcel(T excelDTO);

    private void saveLog(Integer status, String msg, Integer lineId, String skuNo, Integer logId, int count) {
        ExcelBatchOperateDetailLog detailLog = new ExcelBatchOperateDetailLog();
        detailLog.setStatus(status);
        detailLog.setDesc(StringUtils.defaultString(msg));
        detailLog.setLineId(lineId == null ? 0 : lineId);
        detailLog.setSkuNo(skuNo);
        detailLog.setTime(DateUtils.getNowTimeStr());
        detailLog.setLogId(logId);
        logService.insertDetailLog(detailLog);
        logService.increaseNum(logId, count);
    }

    @Override
    public abstract Integer getType();
}
